/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simpleGame.server;

import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.simsilica.es.EntityData;
import com.simsilica.es.EntityId;
import de.tsajar.es.arrow.ArrowComponent;
import de.tsajar.es.character.CharacterControlComponent;
import de.tsajar.es.character.WalkDirectionComponent;
import de.tsajar.es.expiration.ExpirationComponent;
import de.tsajar.es.input.PlayerInputComponent;
import de.tsajar.es.physics.SolidComponent;
import de.tsajar.es.physics.VelocityComponent;
import de.tsajar.es.position.PositionComponent;
import de.tsajar.es.position.RotationComponent;
import de.tsajar.es.render.RenderComponent;

/**
 *
 * @author eike
 */
public class EntityFactory {

    public static EntityData ed;
    public static Vector3f startPoint = new Vector3f(0, 10, 0);

    public static EntityId addScene() {

        EntityId id = ed.createEntity();
        ed.setComponents(id, new RenderComponent("scene"),
                new PositionComponent(Vector3f.ZERO),
                new RotationComponent(Quaternion.DIRECTION_Z),
                new SolidComponent());
        return id;
    }

    public static EntityId addPlayerWithInput(int inputId) {
        EntityId id = ed.createEntity();
        ed.setComponents(id, new PositionComponent(startPoint),
                new RotationComponent(Quaternion.DIRECTION_Z),
                new RenderComponent("player"),
                new PlayerInputComponent(inputId),
                new CharacterControlComponent());
        return id;
    }
    public static EntityId addPlayer(int inputId) {
        EntityId id = ed.createEntity();
        ed.setComponents(id, new PositionComponent(startPoint),
                new RotationComponent(Quaternion.DIRECTION_Z),
//                new RenderComponent("player"),
                new CharacterControlComponent(),
                new PlayerInputComponent(inputId),
                new WalkDirectionComponent(new Vector3f(0,0,1)));
        return id;
    }
    public static EntityId fireArrow(Vector3f location, Vector3f direction) {
        EntityId id = ed.createEntity();
        ed.setComponent(id, new PositionComponent(location));
        ed.setComponent(id, new RotationComponent(Quaternion.DIRECTION_Z));
        ed.setComponent(id, new RenderComponent("arrow"));
        ed.setComponent(id, new ArrowComponent());
        Vector3f randVec = new Vector3f((float) Math.random(), (float) Math.random(), (float) Math.random());
        randVec.subtractLocal(Vector3f.UNIT_XYZ.mult(0.5f));
        randVec.multLocal(3);
        randVec.multLocal(randVec.mult(randVec));
        float error = 0.001f;
        ed.setComponent(id, new VelocityComponent(direction.add(randVec.mult(error)).mult(45)));
        ed.setComponent(id, new ExpirationComponent(30));
        return id;
    }
}
